<?php

namespace app\cps\controller;

use app\cps\service\LaunchAccountService;
use think\facade\Request;
use app\common\Common;

/**
 * 账户管理
 */
class Account extends Base
{
    private $service;

    public function __construct(LaunchAccountService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 添加投放账户
    public function create()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Link::class)->scene('create_account')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->edit($params);

        if (!is_numeric($result) || isset($result['message'])) {
            $message = isset($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 修改投放账户
    public function update()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Link::class)->scene('update_account')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->edit($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染列表页面
    public function index()
    {
        return view();
    }

    // 渲染详情页面
    public function info()
    {
        return view();
    }

    // 渲染订单列表
    public function orderIndex()
    {
        return view();
    }

    // 投放账户列表
    public function list()
    {
        $params = Request::param();
        $data = $this->service->getLaunchAccountList($params);
        return Common::success($data);
    }

    // 投放账户详情
    public function detail()
    {
        $params = Request::param();
        $data = $this->service->getLaunchAccountDetail($params['id']);
        return Common::success($data);
    }

    // 合作公司下拉列表
    public function companySelectList()
    {
        $data = $this->service->getCompanySelectList();
        return Common::success($data);
    }

    // 订单列表
    public function orderList()
    {
        $params = Request::param();

        $data = $this->service->getOrderList($params);
        return Common::success($data);
    }

    // 渲染订单列表
    public function batch()
    {
        return view();
    }

    // 同步
    public function sync()
    {
        $result = $this->service->batchSyncAccount();

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    // 批量更新
    public function batchUpdate()
    {
        $params = Request::param();

        $result = $this->service->batchUpdateAccount($params);

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }
}